var prefix = jsctx + "/tenantSetting";

var editTenantSetting = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmTenantSetting")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateTenantSetting",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = true
                        parent.layer.msg("操作成功");
                    } else {
                        isajaxsend = true
                        parent.layer.alert(data.message)
                    }

                },
                complete: function () {
                    isajaxsend = false
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantSetting").validate({
            rules: {
                minStartMoney: {
                    required: true,
                    number: true,
                    min: 0.01,
                    max: 99999,
                },
                startMultChargeAccountMin: {
                    required: true,
                    digits: true,
                    min: 0,
                    max: 99999,
                },
                startShareChargeAccountMin: {
                    required: true,
                    digits: true,
                    min: 0,
                    max: 99999
                },
                stopChargAccountMin: {
                    required: true,
                    number: true,
                    min: 0.01,
                    max: 99999
                },
                startChargeAccountMin: {
                    required: true,
                    number: true,
                    min: 0.01,
                    max: 99999
                },
                invoiceEnterpriseAmountMin: {
                    required: true,
                    digits: true,
                    min: 1,
                    max: 99999
                }

            },
            messages: {
                minStartMoney: {
                    required: icon + "请输入线上充值最小起步金额",
                    number: icon + "请输入数字",
                    min: icon + "请输入不小于0.01的数",
                    max: icon + "请输入小于99999的数",
                },
                startMultChargeAccountMin: {
                    required: icon + "请输入一号多充用户最小启动充电余额",
                    digits: icon + "请输入整数",
                    min: icon + "请输入不小于0的数",
                    max: icon + "请输入小于99999的数",
                },
                startShareChargeAccountMin: {
                    required: icon + "请输入共享充电,用户最小启动余额",
                    digits: icon + "请输入整数",
                    min: icon + "请输入不小于0的数",
                    max: icon + "请输入小于99999的数",
                },
                stopChargAccountMin: {
                    required: icon + "请输入余额不足停止充电金额",
                    digits: icon + "请输入数字",
                    min: icon + "请输入不小于0.01的数",
                    max: icon + "请输入小于99999的数",
                },
                startChargeAccountMin: {
                    required: icon + "请输入普通用户最小启动充电余额",
                    number: icon + "请输入数字",
                    min: icon + "请输入不小于0.01的数",
                    max: icon + "请输入小于99999的数",
                },
                invoiceEnterpriseAmountMin: {
                    required: icon + "请输入企业开发票最低金额",
                    digits: icon + "请输入整数",
                    min: icon + "请输入不小于1的数",
                    max: icon + "请输入小于99999的数",
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
